package cn.pconline.autoclub.util;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

/**
 *
 * @author ppc
 */
public class AppUtils {
       //APP状态码
    public static final int STATUS_OK = 0;//正常
    public static final int STATUS_FAIL = 1; //失败
    public static final int STATUS_PERMISSIONS_ERROR = 1001;//权限问题
    public static final int STATUS_DATA_ERROR = 1002; //数据错误
    public static final int STATUS_NOLOGIN_ERROR = 1003;//未登录
    // 身份状态码 （已经加入的TYPE根据车友会自己状态）
    public static final int STATUS_UNAUDITED = -3 ; //未审
    public static final int STATUS_SIGHTSEER = -1;//游客
    public static final int STAUTS_JOINOTHER = -2;//已经申请或者加入其他车友会
    // APP约定type
    public static final int ACTION_TYPE_ASSIGNMENT = 1;//转让会长
    public static final int ACTION_TYPE_SETVICE = 2;//设置副会长
    public static final int ACTION_TYPE_REMOVEJOB = 3;//解除职务
    public static final int ACTION_TYPE_REMOVECLUB =4;//移除车友会
    public static final int ACTION_TYPE_CANCELAPPLY = 5 ;//取消申请审核中车友会申请
    //web接口版本号
    public static final String V1_VERSION = "v1.0";//当前接口版本号1.0
    public static final String V1_VERSION_LATEST = "v1.0(latest)";// 当前接口版本号1.0为最新的版本
    
    public static void setSuccessCode(JSONObject json){
        json.put("code", STATUS_OK);
    }
    public static void setNoLoginCode(JSONObject json){
        json.put("code", STATUS_NOLOGIN_ERROR);
        json.put("message", "用户未登录");
    }
    public static void setDataErrorCode(JSONObject json,String message){
        json.put("code", STATUS_DATA_ERROR);
        json.put("message", message);
    }
     public static void setPermissionsError(JSONObject json,String message){
        json.put("code", STATUS_PERMISSIONS_ERROR);
        json.put("message", message);
    }
    
     public static void printMsg(HttpServletResponse resp, String str, String callback) {
         callback = EncodeUtils.encodeForHTMLFilterNull(callback);
         try {
             java.io.PrintWriter w = resp.getWriter();
             if (StringUtils.isNotBlank(callback)) {
                 w.print(callback + "(" + str + ")");
             } else {
                 w.print(str);
             }
             w.flush();
             w.close();
         } catch (IOException ex) {
             
         }
     }
     
     public static  void printSuccessMsg(HttpServletResponse resp, String msg, JSONObject data, String callback) {
         JSONObject json = new JSONObject();
         json.put("code", AppUtils.STATUS_OK);
         if (StringUtils.isNotEmpty(msg)) {
             json.put("message", msg);
         }
         if (data != null) {
             json.put("data", data);
         }
         printMsg(resp, json.toJSONString(), callback);
     }

     public static void printErrorMsg(HttpServletResponse resp, String msg, String callback) {
         JSONObject json = new JSONObject();
         json.put("code", AppUtils.STATUS_FAIL);
         json.put("message", msg);
         printMsg(resp, json.toJSONString(), callback);
     }
     
     /**
      * 如果能找到需要替换的尺寸图片，直接替换尺寸
      * 如果找不到匹配的尺寸图片
      *    1. "_"个数大于1个时，替换最后"_"与"."之间的内容作为尺寸
      *    2. "_"个数小于等于1个时，直接在"."之前加上"_size"
      * @param url
      * @param defaultSize
      * @param size
      * @return
      */
     public static String converImg(String url, String defaultSize, String size) {
         if (StringUtils.isBlank(url)) {
             return url;
         }
         if (url.indexOf(defaultSize) != -1) {
             return url.replaceAll(defaultSize, size);
         } 
         int countL = count(url, "_");
         int dotD = url.lastIndexOf(".");
         int dotL = url.lastIndexOf("_");
         if(countL > 1) {
             return url.substring(0, dotL) + size + url.substring(dotD, url.length());
         } else {
        	 return url.substring(0, dotD) + size + url.substring(dotD, url.length());
         }
     }
     
     /**
      * 判断字符串中，包括有几个某字符串
      * @param text
      * @param sub
      * @return
      */
     public static int count(String text, String sub){  
         int count =0, start =0;  
         while((start=text.indexOf(sub,start))>=0){  
             start += sub.length();  
             count ++;  
         }  
         return count;  
     }  
}
